package com.yicong.ants.ui.circle.ad;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;

import com.cchao.city.LinkageDialog;
import com.cchao.city.LinkageItem;
import com.cchao.city.adapter.MyLinkageItemAdapter;
import com.cchao.city.bean.City;
import com.cchao.simplelib.Const;
import com.cchao.simplelib.core.CollectionHelper;
import com.cchao.simplelib.core.ImageLoader;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.Router;
import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.model.event.CommonEvent;
import com.cchao.simplelib.ui.activity.BaseTitleBarActivity;
import com.cchao.simplelib.util.CallBacks;
import com.cchao.simplelib.util.StringHelper;
import com.cchao.simplelib.util.UrlUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import com.yicong.ants.Constants;
import com.yicong.ants.R;
import com.yicong.ants.bean.circle.PricingInfoItem;
import com.yicong.ants.databinding.AdPublishActivity2Binding;
import com.yicong.ants.databinding.AdPublishDisplayPositonBinding;
import com.yicong.ants.databinding.CircleAdAddressItemBinding;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.CopyHelper;
import com.yicong.ants.manager.ImageHelper;
import com.yicong.ants.manager.RouterHelper;
import com.yicong.ants.manager.UiAssistant;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.config.ZzConfigs;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.manager.loader.GlideEngine2;
import com.yicong.ants.ui.circle.home.StaticHtmlEditActivity;
import com.yicong.ants.ui.find.YcWebViewActivity;
import com.yicong.ants.utils.JsonUtils;
import com.yicong.ants.view.dialog.AntLinkDialog;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cch
 * @version 2020/8/22
 */
public class AdPublishActivity2 extends BaseTitleBarActivity<AdPublishActivity2Binding> implements View.OnClickListener {
    List<PricingInfoItem> mPricingList;
    PricingInfoItem mCurPricingInfo;
    LocalMedia mVideoMedia;
    String mVideoUploadUrl;
    CallBacks.Int mCompressCallBack;
    int mGender = 2;
    int mStyle = 0;
    float mGenderConsume = 0;
    float mAgeConsume = 0;
    float mAddressConsume = 0;
    Drawable mSelectBg = UiHelper.getDrawable(R.drawable.theme_button_bg);
    Drawable mUnSelectBg = UiHelper.getDrawable(R.drawable.button_edge_ccc);
    View[] mAgeViewArr;
    LinkageDialog mAddressDialog;

    @Override
    protected int getLayout() {
        return R.layout.ad_publish_activity2;
    }

    @Override
    protected void initEventAndData() {
        setTitleText("发布广告");
        addTitleMenuItem(UiAssistant.getAtyMenuText(mContext, "广告效果"), view -> {
            //
            ZzConfigs.toWebActivityByUrl(mContext, "project_ad_effect");
        });
        mDataBind.setClick(this);
        onLoadData();
        mAgeViewArr = new View[]{mDataBind.age18, mDataBind.age25, mDataBind.age35, mDataBind.age50, mDataBind.age51};
        initAddressAdapter();
        initViews();
        CopyHelper.listenClip();
    }

    private void initViews() {
        mDataBind.title.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                mDataBind.titleTextLimit.setText(s.toString().length() + "/48");
            }
        });

        mDataBind.totalTimes.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int count = 0;
                try {
                    count = Integer.parseInt(s.toString());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                if (count > 10000) {
                    showToast("投放次数过大");
                    return;
                }
                updateTotalConsume();
            }
        });

        mDataBind.bidEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int count = 0;
                try {
                    count = Integer.parseInt(s.toString());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                if (count > 10000) {
                    showToast("过大");
                    return;
                }
                updateTotalConsume();
            }
        });

        mDataBind.link.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (StringHelper.isEmpty(s.toString())) {
                    return;
                }
                if (s.toString().contains("api/v2/web/htmlRender?id=")) {
                    mDataBind.createHtml.setText("修改链接内容");
                } else {
                    mDataBind.createHtml.setText("没有链接?创建图文");
                }
            }
        });
    }

    @Override
    public void onEvent(CommonEvent event) {
        super.onEvent(event);
        switch (event.getCode()) {
            case Constants.Event.Circle_Ad_Html_Suc:
                mDataBind.link.setText(StaticHtmlEditActivity.mHtmlLink);
                mDataBind.createHtml.setText("修改链接内容");
                break;

            case Constants.Event.Ant_Link_Copy:
                String antUrl = CopyHelper.extractClipAnts();
                if (StringHelper.isEmpty(antUrl)) {
                    return;
                }

                mDataBind.link.setText(antUrl);
                mDataBind.link2.setText(antUrl);
                break;
        }
    }

    @Override
    protected void onLoadData() {
        showProgress();
        addSubscribe(RetrofitHelper.getApis().pricingInfo2(Collections.emptyMap())
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                hideProgress();
                if (respBean.isCodeFail()) {
                    showToast(respBean.getMsg());
                    return;
                }
                switchView(CONTENT);
                mPricingList = respBean.getData();
                updateBills();
                RxHelper.timerConsumer(500, aLong -> updateSelectBill(mPricingList.get(0)));
            }, RxHelper.getSwitchErrorConsumer(this)));
    }

    private void updateBills() {
        mDataBind.position.removeAllViews();
        for (PricingInfoItem item : mPricingList) {
            AdPublishDisplayPositonBinding binding = DataBindingUtil.inflate(mLayoutInflater
                , R.layout.ad_publish_display_positon, null, false);
            binding.tv.setOnClickListener(v -> {
                //
                updateSelectBill(item);
            });
            binding.setBean(item);
            binding.getRoot().setTag(item.getPosition());
            mDataBind.position.addView(binding.getRoot());
        }
    }

    /**
     * 选择的投放位置
     */
    void updateSelectBill(PricingInfoItem info) {
        mDataBind.setBean(info);
        mCurPricingInfo = info;

        for (int i = 0; i < mDataBind.position.getChildCount(); i++) {
            TextView textView = mDataBind.position.getChildAt(i).findViewById(R.id.tv);
            if (mCurPricingInfo.getName().equals(textView.getText().toString())) {
                textView.setBackground(UiHelper.getDrawable(R.drawable.theme_button_bg));
            } else {
                textView.setBackground(UiHelper.getDrawable(R.drawable.button_edge_ccc));
            }
        }

        /*
        // 【cpc】单次点击收费
        // 【cpm】千次展示收费
        // 【daily】每天展示
        */
        UiHelper.setVisibleElseGone(mDataBind.detailDailyField, false);
        UiHelper.setVisibleElseGone(mDataBind.detailCpcField, false);

        switch (mCurPricingInfo.getBill_type()) {
            case "cpc":
            case "cpm":
                UiHelper.setVisibleElseGone(mDataBind.detailCpcField, true);
                break;
            case "daily":
                UiHelper.setVisibleElseGone(mDataBind.detailDailyField, true);
                break;
        }
        updateTotalConsume();
    }

    void chooseGender(int gender) {
        mDataBind.genderAll.setBackground(gender == 2 ? mSelectBg : mUnSelectBg);
        mDataBind.genderMan.setBackground(gender == 1 ? mSelectBg : mUnSelectBg);
        mDataBind.genderWoman.setBackground(gender == 0 ? mSelectBg : mUnSelectBg);
        mGender = gender;
        if (gender == 2) {
            mGenderConsume = 0;
        } else {
            mGenderConsume = mCurPricingInfo.getGender();
        }
        mDataBind.genderConsume.setText("+ " + mGenderConsume + " 门票");
        updateTotalConsume();
    }

    void toggleAge(View ageView) {
        if (ageView.getTag() == null || ageView.getTag() == "0") {
            ageView.setTag("1");
            ageView.setBackground(mSelectBg);
            // 和 不限按钮的交互
            if (ageView == mDataBind.ageAll) {
                for (View view : mAgeViewArr) {
                    view.setBackground(mUnSelectBg);
                    view.setTag("0");
                }
            } else {
                mDataBind.ageAll.setBackground(mUnSelectBg);
                mDataBind.ageAll.setTag("0");
            }
        } else {
            ageView.setTag("0");
            ageView.setBackground(mUnSelectBg);
        }

        int count = 0;
        for (View view : mAgeViewArr) {
            if (view.getTag() == "1") {
                count++;
            }
        }
        if (count >= 5) {
            mAgeConsume = 0;
        } else if (count >= 4) {
            mAgeConsume = mCurPricingInfo.getAge_amount_4();
        } else if (count >= 3) {
            mAgeConsume = mCurPricingInfo.getAge_amount_3();
        } else if (count >= 2) {
            mAgeConsume = mCurPricingInfo.getAge_amount_2();
        } else if (count >= 1) {
            mAgeConsume = mCurPricingInfo.getAge_amount_1();
        } else {
            mAgeConsume = 0;
        }
        mDataBind.ageConsume.setText("+ " + mAgeConsume + " 门票");
        updateTotalConsume();
    }

    private List<LinkageItem> getCityList() {
        String json = JsonUtils.getJson(this, "city.json");
        Gson gson = new Gson();
        Type type = new TypeToken<ArrayList<City>>() {
        }.getType();
        List<City> citys = gson.fromJson(json, type);
        List<LinkageItem> cityList = new ArrayList<>();
        cityList.addAll(citys);
        return cityList;
    }

    private void initAddressAdapter() {
        //创建对话框
        mAddressDialog = new LinkageDialog.Builder(this, 3)
            .setLinkageData(getCityList())
            .setTitle("投放区域")
            .setTabIndicatorColor(Color.parseColor("#EA6F5A"))
            .setTabTitleColor(Color.BLACK, Color.parseColor("#EA6F5A"))
            .setTabIndicatorHeight((int) getResources().getDimension(R.dimen.indicator_height))
            .setContentHeight((int) (getResources().getDimension(R.dimen.item_city_height) * 8))
            .setOnLinkageAdapterListener(() -> {
                //
                return new MyLinkageItemAdapter(mContext);
            })
            .setOnLinkageSelectListener(items -> {
                String address = "";
                String lastAddressId = "";
                for (LinkageItem item : items) {
                    if (item == null) {
                        continue;
                    }
                    if ("不限".equalsIgnoreCase(item.getLinkageName())) {
                        continue;
                    }
                    address += " " + item.getLinkageName();
                    lastAddressId = item.getLinkageId();
                }
//              UiHelper.showLongToast(items[2].getLinkageId());

                CircleAdAddressItemBinding addressItemBinding = DataBindingUtil.inflate(mLayoutInflater, R.layout.circle_ad_address_item, null, false);
                addressItemBinding.name.setText(address.trim());
                addressItemBinding.getRoot().setTag(lastAddressId);
                addressItemBinding.setClick(v -> {
                    mDataBind.addressFlex.removeView(addressItemBinding.getRoot());
                    updateAddressConsume();
                });
                mDataBind.addressFlex.addView(addressItemBinding.getRoot());
                updateAddressConsume();
            }).build();
    }

    private void updateAddressConsume() {
        int addressFlexChildCount = mDataBind.addressFlex.getChildCount();
        if (addressFlexChildCount >= 4) {
            mAddressConsume = mCurPricingInfo.getAddress_amount_3();
        } else if (addressFlexChildCount >= 3) {
            mAddressConsume = mCurPricingInfo.getAddress_amount_2();
        } else if (addressFlexChildCount >= 2) {
            mAddressConsume = mCurPricingInfo.getAddress_amount_1();
        } else {
            mAddressConsume = 0;
        }
        mDataBind.areaConsume.setText("+ " + mAddressConsume + " 门票");
        updateTotalConsume();
    }

    BigDecimal updateTotalConsume() {
        BigDecimal timesPrice =
            new BigDecimal(String.valueOf(mCurPricingInfo.getBase()));
//            .add(new BigDecimal(String.valueOf(mGenderConsume)))
//            .add(new BigDecimal(String.valueOf(mAgeConsume)))
//            .add(new BigDecimal(String.valueOf(mAddressConsume)));
        mDataBind.timesConsume.setText("+ " + timesPrice + " 门票");

        if (StringHelper.isEmpty(mDataBind.totalTimes.getText())) {
            return timesPrice;
        }
        BigDecimal total = timesPrice.multiply(new BigDecimal(mDataBind.totalTimes.getText().toString()));

        if (StringHelper.isNotEmpty(mDataBind.bidEdit.getText().toString())) {
            total = total.add(new BigDecimal(mDataBind.bidEdit.getText().toString()));
        }

        mDataBind.coinTotal.setText(String.valueOf(total));
        return total;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK || data == null || requestCode != Constants.RequestCode.TAKE_IMAGE) {
            return;
        }

        List<String> pathList = data.getStringArrayListExtra("result");
        if (CollectionHelper.isEmpty(pathList)) {
            return;
        }

        ImageHelper.uploadImage(mDisposable, pathList.get(0), respBean -> {
            if (respBean.isCodeFail()) {
                showError();
                return;
            }
            ImageLoader.loadImage(mDataBind.imageUpload, respBean.getData(), R.drawable.place_holder);
            UiHelper.setVisibleElseGone(mDataBind.imageUploadField, true);
            UiHelper.setVisibleElseGone(mDataBind.imageAdd, false);
            mDataBind.imageUpload.setTag(respBean.getData());
        }, throwable -> {
            showError();
            Logs.logException(throwable);
        });
    }

    void onUploadVideo() {

        ImageHelper.uploadFile(mDisposable, ImageHelper.mCompressOutputPath, respBean -> {
            hideProgress();
            if (respBean.isCodeFail()) {
                showToast(respBean.getMsg());
                return;
            }
            mVideoUploadUrl = respBean.getData().getFile_url();
        }, throwable -> {
            hideProgress();
            Logs.logException(throwable);
            showToast("上传失败，请检查后再试");
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                onBackPressed();
                break;
            case R.id.gender_all:
                chooseGender(2);
                break;
            case R.id.gender_man:
                chooseGender(1);
                break;
            case R.id.gender_woman:
                chooseGender(0);
                break;
            case R.id.image_add:
                if (mDataBind.imageUpload.getTag() != null) {
                    showToast("您只能添加一张图片或视频");
                    return;
                }

                if ("image".equals(mCurPricingInfo.getDisplay_type())) {
//                    PictureSelector.create(this)
//                        .openGallery(SelectMimeType.ofImage())
//                        .selectionMode(PictureConfig.SINGLE)
//                        .isEnableCrop(true)
//                        .cropImageWideHigh(mCurPricingInfo.getDisplay_width(), mCurPricingInfo.getDisplay_width())
//                        .withAspectRatio(mCurPricingInfo.getDisplay_height(), mCurPricingInfo.getDisplay_height())
//                        .freeStyleCropEnabled(true)
//                        .imageEngine(GlideEngine2.createGlideEngine())
//                        .forResult(new OnResultCallbackListener<LocalMedia>() {
//                            @Override
//                            public void onResult(List<LocalMedia> result) {
//                                if (CollectionHelper.isEmpty(result)) {
//                                    return;
//                                }
//                                // 结果回调
//                                ImageHelper.uploadImage(mDisposable, result.get(0).getCutPath(), respBean -> {
//                                    if (respBean.isCodeFail()) {
//                                        showError();
//                                        return;
//                                    }
//                                    ImageHelper.loadImageRound(mDataBind.imageUpload, respBean.getData(), 2);
//                                    UiHelper.setVisibleElseGone(mDataBind.imageUploadField, true);
//                                    UiHelper.setVisibleElseGone(mDataBind.videoField, false);
//                                    UiHelper.setVisibleElseGone(mDataBind.imageAdd, false);
//                                    mDataBind.imageUpload.setTag(respBean.getData());
//                                }, throwable -> {
//                                    showError();
//                                    Logs.logException(throwable);
//                                });
//                            }
//
//                            @Override
//                            public void onCancel() {
//                                // 取消
//                            }
//                        });
                } else if ("video".equals(mCurPricingInfo.getDisplay_type())) {
                    ImageHelper.addShortVideo(thisActivity, localMediaList -> {
                        mVideoMedia = localMediaList.get(0);

                        String imagePath = mVideoMedia.getRealPath();
                        if (StringHelper.isEmpty(imagePath)) {
                            imagePath = mVideoMedia.getParentFolderName();
                        }
                        ImageHelper.loadImageRound(mDataBind.imageUpload, imagePath);
                        UiHelper.setVisibleElseGone(mDataBind.imageUploadField, true);
                        UiHelper.setVisibleElseGone(mDataBind.videoField, true);
                        UiHelper.setVisibleElseGone(mDataBind.imageAdd, false);
                        UiHelper.setVisibleElseGone(mDataBind.play, false);
                        mDataBind.imageUpload.setTag(localMediaList);

                        BusinessHelper.compressVideo2(imagePath, mCompressCallBack = intValue -> {
                            mDataBind.compressProgress.setProgress(intValue);
                            mDataBind.compressText.setText(intValue + "%");
                            UiHelper.setVisibleElseGone(mDataBind.compressProgress, intValue < 100);
                            UiHelper.setVisibleElseGone(mDataBind.compressText, intValue < 100);
                            UiHelper.setVisibleElseGone(mDataBind.play, intValue >= 100);
                            if (intValue >= 200) {
                                onUploadVideo();
                            }
                        });
                    });
                }

                break;

            case R.id.play:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                String type = "video/mp4";
                intent.setDataAndType(Uri.fromFile(new File(mVideoMedia.getRealPath())), type);
                startActivity(intent);
                break;
            case R.id.image_close:
                mDataBind.imageUpload.setTag(null);
                UiHelper.setVisibleElseGone(mDataBind.imageAdd, true);
                UiHelper.setVisibleElseGone(mDataBind.imageUploadField, false);
                break;
            case R.id.address_add:
                if (mDataBind.addressFlex.getChildCount() >= 4) {
                    showToast("最多添加3个地址");
                    return;
                }
                mAddressDialog.show();
                break;
            case R.id.create_html:
                try {
                    if (StringHelper.isNotEmpty(mDataBind.link) && mDataBind.link.getText().toString().contains("api/v2/web/htmlRender?id=")) {
                        String htmlId = UrlUtil.getValueFromUrl(mDataBind.link.getText().toString(), "id");
                        Router.turnTo(mContext, StaticHtmlEditActivity.class)
                            .putExtra(Constants.Extra.Id, htmlId)
                            .start();
                        return;
                    }
                } catch (Exception exception) {
                    showToast(exception.getMessage());
                }
                Router.turnTo(mContext, StaticHtmlEditActivity.class).start();
                break;
            case R.id.review:
                if (StringHelper.isEmpty(mDataBind.title)) {
                    showToast("请填写标题");
                    return;
                }
                if (mDataBind.imageUpload.getTag() == null) {
                    showToast("请上传图片");
                    return;
                }
                Dialogs.showAdReview(mContext, mStyle, mDataBind.title.getText().toString(), String.valueOf(mDataBind.imageUpload.getTag()), null);
                break;
            case R.id.age_all:
            case R.id.age_18:
            case R.id.age_25:
            case R.id.age_35:
            case R.id.age_50:
            case R.id.age_51:
                toggleAge(v);
                break;
            case R.id.apply:
                Map<String, String> map = getQueryMap();
                if (CollectionHelper.isEmpty(map)) {
                    return;
                }
                addSubscribe(RetrofitHelper.getApis().publishAd(map)
                    .compose(RxHelper.toMain())
                    .subscribe(respBean -> {
                        hideProgress();
                        if (respBean.isCodeFail()) {
                            showToast(respBean.getMsg());
                            return;
                        }
                        Dialogs.showTaskSuc(mContext, "发布广告成功", "您的广告将在审核通过后由系统投放", () -> {
                            //
                            finish();
                        });
                    }, RxHelper.getHideProgressConsumer(this)));
                break;
            case R.id.position_preview:
                Dialogs.showAdPreview(thisActivity, mCurPricingInfo);
                break;
            case R.id.add_mall_link:
                Router.turnTo(mContext, YcWebViewActivity.class)
                    .putExtra(Const.Extra.Web_View_Url, Constants.getWebUrl("mall"))
                    .putExtra(Const.Extra.Web_View_Tile, "蚁丛商城")
                    .putExtra(Const.Extra.Web_View_Title_Visible, false)
                    .checkLogin(true)
                    .start();
                break;
            case R.id.tv_ant_link:
                AntLinkDialog.newInstance(this).show(getSupportFragmentManager());
                break;
            case R.id.cur_ranking:
            case R.id.rank_q:
                Map<String, String> map2 = getQueryMap();
                if (CollectionHelper.isEmpty(map2)) {
                    return;
                }
                showProgress();
                addSubscribe(RetrofitHelper.getApis().queryRank(map2)
                    .compose(RxHelper.toMain())
                    .subscribe(respBean -> {
                        hideProgress();
                        if (respBean.isCodeFail()) {
                            showToast(respBean.getMsg());
                            return;
                        }
                        mDataBind.curRanking.setText(respBean.getData().get("rank"));
                    }, RxHelper.getHideProgressConsumer(this)));
                break;
            case R.id.link_preview:
            case R.id.link_preview2:
                TextView linkTv = mDataBind.link;
                if (mCurPricingInfo.isMall_product_position()) {
                    linkTv = mDataBind.link2;
                }

                if (StringHelper.isEmpty(linkTv.getText())) {
                    showToast("请先选择商城商品");
                    return;
                }

                String ilink = linkTv.getText().toString();
                if (!RouterHelper.tryRoutePlatformUri(thisActivity, ilink)) {
                    Router.turnTo(this, YcWebViewActivity.class)
                        .putExtra(Const.Extra.Web_View_Url, ilink)
                        .putExtra(Const.Extra.Web_View_Tile, "预览内容")
                        .putExtra(Const.Extra.Web_View_Title_Visible, false)
                        .checkLogin(true)
                        .start();
                }
                break;
            case R.id.tv_how_create:
            case R.id.tv_how_create2:
                //跳转web页，后续会给出url地址
                ZzConfigs.toWebActivityByUrlNotLogin(this, "project_ant_link");
                break;
        }
    }

    private Map<String, String> getQueryMap() {
        if (StringHelper.isEmpty(mDataBind.title)) {
            showToast("请填写标题");
            return null;
        }

        switch (mCurPricingInfo.getDisplay_type()) {
            case "image":
                if (mDataBind.imageUpload.getTag() == null) {
                    showToast("请上传图片");
                    return null;
                }
                break;
            case "video":
                if (mVideoMedia != null && StringHelper.isEmpty(mVideoUploadUrl)) {
                    showToast("视频上传中");
                    onUploadVideo();
                    return null;
                }
                break;
        }
        if (StringHelper.isEmpty(mDataBind.link)) {
            showToast("请填写链接");
            return null;
        }

        if (StringHelper.isEmpty(mDataBind.totalTimes)) {
            showToast("请填写投放次数");
            return null;
        }

        if (Integer.parseInt(mDataBind.totalTimes.getText().toString()) < mCurPricingInfo.getMin()) {
            showToast("投放次数小于最低投放数 " + mCurPricingInfo.getMin());
            return null;
        }

        showProgress();
        Map<String, String> map = new HashMap<>();
        map.put("title", mDataBind.title.getText().toString());
        if (mCurPricingInfo.isMall_product_position()) {
            map.put("display_type", "mall_product");
        } else {
            switch (mCurPricingInfo.getDisplay_type()) {
                case "image":
                    map.put("display", mDataBind.imageUpload.getTag().toString());
                    map.put("display_type", "image");
                    break;
                case "video":
                    map.put("display", mVideoUploadUrl);
                    map.put("display_type", "video");
                    break;
            }
        }

        map.put("link", mDataBind.link.getText().toString());
        map.put("total_times", mDataBind.totalTimes.getText().toString());
        map.put("gender", String.valueOf(mGender));
        map.put("style", String.valueOf(mStyle));
        map.put("position", mCurPricingInfo.getPosition());
        map.put("bill_price", StringHelper.isEmpty(mDataBind.bidEdit.getText()) ? "0" : mDataBind.bidEdit.getText().toString());

        // 年龄
        List<String> arr = new ArrayList<>();
        if (mDataBind.age18.getTag() == "1") {
            arr.add("18");
        }
        if (mDataBind.age25.getTag() == "1") {
            arr.add("25");
        }
        if (mDataBind.age35.getTag() == "1") {
            arr.add("35");
        }
        if (mDataBind.age50.getTag() == "1") {
            arr.add("50");
        }
        if (mDataBind.age51.getTag() == "1") {
            arr.add("18");
        }
        if (CollectionHelper.isEmpty(arr)) {
            map.put("age", "18,25,35,50,51");
        } else {
            map.put("age", StringUtils.join(arr.toArray(), ","));
        }
        for (int i = 1; i < mDataBind.addressFlex.getChildCount(); i++) {
            map.put("address_" + i, mDataBind.addressFlex.getChildAt(i).getTag().toString());
        }
        return map;
    }
}
